﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YJKExcelFormTool.Creator;
using YJKExcelFormTool.DataDefine;
using YJKExcelFormTool.Kits;

namespace YJKExcelFormTool.Forms
{
	public partial class GraphPaperDirectoryForm : System.Windows.Forms.Form
	{
		public TableExportKit ExportKit
		{
			get
			{
				return this.m_ExportKit;
			}
		}

		public bool IsDrawingToRevit
		{
			get
			{
				return this.m_IsDrawingToRevit;
			}
		}

		public TableInfos TheTableInfos
		{
			get
			{
				return this.m_CommonForm.TheTableInfos;
			}
		}

		public GraphPaperDirectoryForm(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
			this.InitializeComponent();
			this.InitData();
		}

		private void btDrawing_Click(object sender, EventArgs e)
		{
			if (this.m_CommonForm.TheTableInfos.IsNull())
			{
				YJKMessageBox.Show("当前表头数据为空，请设置表头数据后，重新绘制。");
				base.DialogResult = DialogResult.No;
				return;
			}
			base.DialogResult = DialogResult.OK;
			this.m_IsDrawingToRevit = true;
		}

		private void btExport_Click(object sender, EventArgs e)
		{
			if (this.m_CommonForm.TheTableInfos.IsNull())
			{
				YJKMessageBox.Show("当前表头数据为空，请设置表头数据后，重新输出。");
				base.DialogResult = DialogResult.No;
				return;
			}
			string fontName = this.TheTableInfos.FontName();
			int fontSize = (int)(this.TheTableInfos.FontHeight() / 0.353);
			using (ExportExcelSettingForm exportExcelSettingForm = new ExportExcelSettingForm(ExportExcelSettingForm.ExportType.GraphPaperDirectory, fontName, fontSize))
			{
				exportExcelSettingForm.SetPageRowInfo(this.m_CommonForm.TheTableInfos.Head.RowSize, this.m_CommonForm.TheTableInfos.Head.TableSourceInfo.BadyEndRowNumber);
				if (exportExcelSettingForm.ShowDialog(this) != DialogResult.OK)
				{
					base.DialogResult = DialogResult.No;
					return;
				}
				TableExportKit.FontSetting fontSetting = default(TableExportKit.FontSetting);
				fontSetting.name = exportExcelSettingForm.FontName;
				fontSetting.size = exportExcelSettingForm.FontSize;
				fontSetting.alignmentH = (TableExportKit._tagHorizontalAlignment)exportExcelSettingForm.FontHorizontalalignment;
				fontSetting.alignmentV = (TableExportKit._tagVerticalAlignment)exportExcelSettingForm.FontVerticalAlignment;
				this.m_CommonForm.TheTableInfos.Head.TableSourceInfo.BadyEndRowNumber = exportExcelSettingForm.EndRowInfo;
				this.m_ExportKit = new TableExportKit(this.m_CommonForm.TheTableInfos, exportExcelSettingForm.UsingDefaultFontSetting, fontSetting);
			}
			base.DialogResult = DialogResult.OK;
			this.m_IsDrawingToRevit = false;
		}

		private void GraphPaperDirectoryForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (base.DialogResult == DialogResult.OK)
			{
				string message = "";
				if (!this.SaveData(ref message))
				{
					YJKMessageBox.Show(message);
					e.Cancel = true;
					return;
				}
			}
			else if (base.DialogResult == DialogResult.No)
			{
				e.Cancel = true;
			}
		}

		private void tSMISelectAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.cklbTablePaper.Items.Count; i++)
			{
				this.cklbTablePaper.SetItemChecked(i, true);
			}
		}

		private void tSMISelectNull_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.cklbTablePaper.Items.Count; i++)
			{
				this.cklbTablePaper.SetItemChecked(i, false);
			}
		}

		private void tSMIContrary_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.cklbTablePaper.Items.Count; i++)
			{
				bool itemChecked = this.cklbTablePaper.GetItemChecked(i);
				this.cklbTablePaper.SetItemChecked(i, !itemChecked);
			}
		}

		private void InitData()
		{
			string sourceFileName = AssistFunc.GetAppRootPath() + "\\Data\\GraphPaperDirectoryTableSetting.xml";
			string text = Product.WorkingLocation + "GraphPaperDirectoryTableSetting.xml";
			if (!File.Exists(text))
			{
				this.CopyFile(sourceFileName, text);
			}
			string sourceFileName2 = AssistFunc.GetAppRootPath() + "\\Data\\标准图纸表.xml";
			string text2 = Product.WorkingLocation + "标准图纸表.xml";
			if (!File.Exists(text2))
			{
				this.CopyFile(sourceFileName2, text2);
			}
			List<string> list = new List<string>();
			list.Add("序号");
			list.Add("比例");
			list.Add("绘图员");
			list.Add("设计者");
			list.Add("审核者");
			list.Add("审图员");
			list.Add("图纸编号");
			list.Add("图纸发布日期");
			list.Add("图纸名称");
			list.Add("图幅");
			Dictionary<string, string> dict = this.InitColumnDict();
			this.m_CommonForm = new TableCommonForm(this.m_Revit, text, text2, "图纸目录框", "图纸目录", list, dict);
			this.m_CommonForm.TopLevel = false;
			this.m_CommonForm.Dock = DockStyle.Fill;
			this.m_CommonForm.FormBorderStyle = FormBorderStyle.None;
			this.pnCommnForm.Controls.Add(this.m_CommonForm);
			this.m_CommonForm.Show();
			this.m_GraphPaperList = GraphPaperDirectoryCreator.GetViewSheetList(this.m_Revit);
			int num = 0;
			foreach (ViewSheet viewSheet in this.m_GraphPaperList)
			{
				this.cklbTablePaper.Items.Add(viewSheet.Name);
				this.cklbTablePaper.SetItemChecked(num, true);
				num++;
			}
		}

		private void CopyFile(string sourceFileName, string destFileName)
		{
			try
			{
				File.Copy(sourceFileName, destFileName);
			}
			catch (Exception)
			{
			}
		}

		private bool SaveData(ref string message)
		{
			if (!this.m_CommonForm.SaveData(ref message))
			{
				return false;
			}
			GraphPaperDirectoryCreator graphPaperDirectoryCreator = new GraphPaperDirectoryCreator(this.m_Revit);
			this.m_CommonForm.TheTableInfos.Body = graphPaperDirectoryCreator.CreateTableBody(this.m_CommonForm.TheTableInfos.EffectiveColumnParameterList(), this.SelectViewSheets());
			return true;
		}

		private List<ViewSheet> SelectViewSheets()
		{
			List<ViewSheet> list = new List<ViewSheet>();
			for (int i = 0; i < this.cklbTablePaper.Items.Count; i++)
			{
				if (this.cklbTablePaper.GetItemChecked(i))
				{
					list.Add(this.m_GraphPaperList[i]);
				}
			}
			return list;
		}

		private Dictionary<string, string> InitColumnDict()
		{
			return new Dictionary<string, string>
			{
				{
					"序号",
					"序号"
				},
				{
					"比例",
					"比例"
				},
				{
					"绘图员",
					"绘图员"
				},
				{
					"设计者",
					"设计者"
				},
				{
					"审核者",
					"审核者"
				},
				{
					"审图员",
					"审图员"
				},
				{
					"图纸编号",
					"图纸编号"
				},
				{
					"图纸发布日期",
					"图纸发布日期"
				},
				{
					"图纸名称",
					"图纸名称"
				},
				{
					"图幅",
					"图幅"
				}
			};
		}

		private ExternalCommandData m_Revit;

		private TableCommonForm m_CommonForm;

		private bool m_IsDrawingToRevit;

		private List<ViewSheet> m_GraphPaperList = new List<ViewSheet>();

		private TableExportKit m_ExportKit;
	}
}
